package com.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.connect.DBconnect;
import com.toolbean.MyTools;
import com.valuebean.*;

public class DoShop extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
        doPost(request,response);
	
	}
public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
        String action=request.getParameter("action");
        if(action==null){
        	action="";
        }
        if(action.equals("select")){
        	this.select(request,response);
        }
        if(action.equals("insert")){
        	this.insert(request,response);
        }
        if(action.equals("delete")){
        	this.delete(request,response);
        }
        if(action.equals("update")){
        	this.update(request,response);
        }
	}
public void select(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String driverClass = "com.mysql.jdbc.Driver";
		String user = "root";
        String pwd = "123456";
		String url = "jdbc:mysql://localhost:3306/shopdb?useUnicode=true&characterEncoding=utf-8";	
		Connection con=null;
		PreparedStatement pst = null;
		Statement st = null;
		ResultSet rs= null;
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
		MyTools mytools=new MyTools();
		ArrayList list = new ArrayList();
		RequestDispatcher rdd=null;
		DBconnect dbcon = new DBconnect(driverClass,url,pwd,user);
		try {
			con = dbcon.getConnection();
			String sql = "select  *from  shop";
		     st = con.createStatement();
			 rs=st.executeQuery(sql);
			 while(rs.next()){
				  shop shop = new shop();
				  shop.setShopname(rs.getString("shopname"));
				  shop.setPrice(rs.getFloat("price"));
				  shop.setClasses(rs.getString("classes"));
				  shop.setNum(rs.getInt("num"));
				  shop.setShopinformation(rs.getString("shopinformation"));
				  list.add(shop);
			 }
			request.setAttribute("list", list);
			rdd=request.getRequestDispatcher("admin/select.jsp");
			rdd.forward(request, response);
		} catch (SQLException e) {
			e.printStackTrace();
		}
    }
public void insert(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String driverClass = "com.mysql.jdbc.Driver";
		String user = "root";
        String pwd = "123456";
		String url = "jdbc:mysql://localhost:3306/shopdb?useUnicode=true&characterEncoding=utf-8";	
		Connection con=null;
		PreparedStatement pst = null;
		Statement st = null;
		ResultSet rs= null;
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
		MyTools mytools=new MyTools();
		 String shopname=mytools.toChinese(request.getParameter("shopname"));
		 String price1=mytools.toChinese(request.getParameter("price"));
		 String num1=mytools.toChinese(request.getParameter("num"));
		 String classes=mytools.toChinese(request.getParameter("classes"));
		 String shopinformation=mytools.toChinese(request.getParameter("shopinformation"));
		DBconnect dbcon = new DBconnect(driverClass,url,pwd,user);
		try {
			float price=Float.parseFloat(price1);
			int num=Integer.parseInt(num1);
			con = dbcon.getConnection();
			String sql="insert into shop(shopname,price,num,classes,shopinformation)values(?,?,?,?,?)";
		     pst = con.prepareStatement(sql);
			 pst.setString(1,shopname);
			 pst.setFloat(2,price);
			 pst.setInt(3,num);
			 pst.setString(4,classes);
			 pst.setString(5,shopinformation);
			 int a=pst.executeUpdate();
			 if(a==1){
				 out.println("��ӳɹ�");
				  
			 }else{
				 out.println("���ʧ��");
			 }
		} catch (SQLException e) {
			e.printStackTrace();
		}
    }
public void delete(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String driverClass = "com.mysql.jdbc.Driver";
		String user = "root";
        String pwd = "123456";
		String url = "jdbc:mysql://localhost:3306/shopdb?useUnicode=true&characterEncoding=utf-8";	
		Connection con=null;
		PreparedStatement pst = null;
		Statement st = null;
		ResultSet rs= null;
		HttpSession session1=request.getSession();
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
		MyTools mytools=new MyTools();
		String id1=mytools.toChinese(request.getParameter("id"));
		int id=Integer.parseInt(id1);
		RequestDispatcher rdd=null;
		int a=0;
		DBconnect dbcon = new DBconnect(driverClass,url,pwd,user);
		try {
			con = dbcon.getConnection();
		     pst = con.prepareStatement("delete from shop where shopid=?");
			 pst.setInt(1,id);
			 a=pst.executeUpdate();
			 if(a==1){
				 System.out.println("Success");
			 }else{
				 System.out.println("failed");
			 }
			 rdd=request.getRequestDispatcher("admin/delete.jsp");
			 rdd.forward(request, response);
			 
	      }catch (SQLException e) {
				e.printStackTrace();
			}
	    }
public void update(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String driverClass = "com.mysql.jdbc.Driver";
		String user = "root";
        String pwd = "123456";
		String url = "jdbc:mysql://localhost:3306/shopdb?useUnicode=true&characterEncoding=utf-8";	
		Connection con=null;
		PreparedStatement pst = null;
		Statement st = null;
		ResultSet rs= null;
		HttpSession session=request.getSession();
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
		MyTools mytools=new MyTools();
		String id1=(String)session.getAttribute("id");
		int id=Integer.parseInt(id1);
		System.out.println(id);
		 String shopname=mytools.toChinese(request.getParameter("shopname"));
		 String price1=mytools.toChinese(request.getParameter("price"));
		 String num1=mytools.toChinese(request.getParameter("num"));
		 String classes=mytools.toChinese(request.getParameter("classes"));
		 String shopinformation=mytools.toChinese(request.getParameter("shopinformation"));
		DBconnect dbcon = new DBconnect(driverClass,url,pwd,user);
		try {
			float price=Float.parseFloat(price1);
			int num=Integer.parseInt(num1);
			con = dbcon.getConnection();
		     pst = con.prepareStatement("update shop set shopname=?,price=?,num=?,classes=?,shopinformation=? where shopid=?");
			 pst.setString(1,shopname);
			 pst.setFloat(2,price);
			 pst.setInt(3,num);
			 pst.setString(4,classes);
			 pst.setString(5,shopinformation);
			 pst.setInt(6,id);
			 int a=pst.executeUpdate();
			 if(a==1){
				 out.println("更新成功");
				  
			 }else{
				 out.println("更新失败");
			 }
		} catch (SQLException e) {
			e.printStackTrace();
		}
    }


}
